package com.dsteiner.xelavos.client.domain;

import java.util.Date;

import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.dsteiner.widgets.client.ObjectUtil;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.domain.Searchable;
import com.dsteiner.widgets.client.domain.SearchableId;
import com.dsteiner.widgets.client.domain.SearchableProperty;
import com.dsteiner.widgets.client.domain.SearchableRefId;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Searchable
public class Comment extends ModelObject implements Rateable{

	public Comment() {
	}
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    @SearchableId
    private String id;
	
	public String getId(){
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

    @Persistent
	private String userId;
    
    @Persistent
    @SearchableProperty
    private String userFirstName;
    
    @Persistent
    private int ratingTotal;
    
    @Persistent
    private int ratingCount;
    
    @Persistent
    @SearchableProperty
	private String message;
	
    @Persistent
    @SearchableRefId
	private String refId;
    
    @Persistent
	private String refClass;
    
    @Persistent
    @SearchableProperty
    private Date registeredDate;

    public Date getRegisteredDate() {
		return registeredDate;
	}
    public void setRegisteredDate(Date registeredDate) {
		this.registeredDate = registeredDate;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public void addRating(int rating) {
		ratingCount++;
		ratingTotal += rating;
	}
	public int getRating() {
		return ratingTotal;
	}
	public int getRatingCount() {
		return ratingCount;
	}
	public int getRatingTotal() {
		return ratingTotal;
	}
	public void setRatingCount(int ratingCount) {
		this.ratingCount = ratingCount;
	}
	public void setRatingTotal(int ratingTotal) {
		this.ratingTotal = ratingTotal;
	}
	public String getUserFirstName() {
		return userFirstName;
	}
	public void setUserFirstName(String userFirstName) {
		this.userFirstName = userFirstName;
	}
	public void removeRating(int rating) {
		ratingCount--;
		ratingTotal -= rating;
	}
	public String getRefClass() {
		return refClass;
	}
	public String getRefId() {
		return refId;
	}
	public void setRefClass(String refClass) {
		this.refClass = refClass;
	}
	public void setRefId(String refId) {
		this.refId = refId;
	}
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (!(obj instanceof Comment))
			return false;
		Comment other = (Comment) obj;
		return ObjectUtil.equals(other.id, id);
	}
}
